package fromen.accelerometermapping;

import android.content.Context;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

public class MainActivity extends ActionBarActivity implements SensorEventListener
{
    //Variables used for mapping
    double xCoord, yCoord, xVel, yVel, xAccel, yAccel, lastUpdate, timeHolder;

    //Text fields to change
    EditText xCoordET, yCoordET, xVelET, yVelET, xAccelET, yAccelET, xOffsetET, yOffsetET, lastUpdateET;
    Button calibrateBTN;


    //Variables for calibration
    boolean calibrationNeeded = true;
    double xOffset, yOffset;
    int sampleNumber = 0;

    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        //Accelerometer declaration
        SensorManager sm = (SensorManager) getSystemService(Context.SENSOR_SERVICE);
        Sensor accelmeter = sm.getSensorList(Sensor.TYPE_ACCELEROMETER).get(0);
        sm.registerListener(this, accelmeter, SensorManager.SENSOR_DELAY_GAME);

        xCoord = yCoord = xVel = yVel = xAccel = yAccel = 0.0;

        //Definitions for text fields
        xAccelET = (EditText) findViewById(R.id.xAccelEditText);
        yAccelET = (EditText) findViewById(R.id.yAccelEditText);
        xVelET = (EditText) findViewById(R.id.xVelEditText);
        yVelET = (EditText) findViewById(R.id.yVelEditText);
        xCoordET = (EditText) findViewById(R.id.xCoordEditText);
        yCoordET = (EditText) findViewById(R.id.yCoordEditText);
        xOffsetET = (EditText) findViewById(R.id.xOffsetEditText);
        yOffsetET = (EditText) findViewById(R.id.yOffsetEditText);
        lastUpdateET = (EditText) findViewById(R.id.lastUpdateEditText);
        calibrateBTN = (Button) findViewById(R.id.calibrateButton);

        calibrateBTN.setOnClickListener(calibrateListener);
    }

    public View.OnClickListener calibrateListener = new View.OnClickListener()
    {
        @Override
        public void onClick(View v)
        {
            calibrationNeeded = true;
        }
    };

    public void Calibrate(double xMeterAccel, double yMeterAccel, long time)
    {
        //Offset calibration

        sampleNumber += 1;

        xOffset += xMeterAccel;
        yOffset += yMeterAccel;

        //Last run of calibration
        int maxSamples = 200;
        if (sampleNumber >= maxSamples)
        {
            calibrationNeeded = false;
            xOffset = xOffset / sampleNumber;
            yOffset = yOffset / sampleNumber;
            sampleNumber = 0;

            xCoord = yCoord = xVel = yVel = xAccel = yAccel = 0.0;

            timeHolder = time;
        }

        //Display calibrating message
        xAccelET.setText(String.format("%.09f", xMeterAccel));
        yAccelET.setText(String.format("%.09f", yMeterAccel));

        xVelET.setText("Calibrating...");
        yVelET.setText("Calibrating...");

        xCoordET.setText(Integer.toString(sampleNumber));
        yCoordET.setText("Calibrating...");

        xOffsetET.setText(String.format("%.09f", xOffset));
        yOffsetET.setText(String.format("%.09f", yOffset));
    }

    @Override
    public void onSensorChanged(SensorEvent event)
    {
        if(calibrationNeeded)
        {
            Calibrate(event.values[0], event.values[1], event.timestamp);
        }
        else
        {
            lastUpdate = (event.timestamp - timeHolder) / 1000000000;
            timeHolder = event.timestamp;

            xAccel = event.values[0] - xOffset;
            yAccel = event.values[1] - yOffset;

            //Math for mapping values
            xVel += xAccel * lastUpdate;
            yVel += yAccel * lastUpdate;

            xCoord += xVel * lastUpdate;
            yCoord += yVel * lastUpdate;

            //Display values
            xAccelET.setText(String.format("%.09f", xAccel));
            yAccelET.setText(String.format("%.09f", yAccel));

            xVelET.setText(String.format("%.09f", xVel));
            yVelET.setText(String.format("%.09f", yVel));

            xCoordET.setText(String.format("%.09f", xCoord));
            yCoordET.setText(String.format("%.09f", yCoord));

            lastUpdateET.setText(String.format("%.09f", lastUpdate));
        }
    }

    @Override
    public void onAccuracyChanged(Sensor sensor, int accuracy)
    {

    }
}
